package com.vicene.gmail.order.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.vicene.gmail.order.entity.OrderItemEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.vicene.gmail.order.entity.OrderEntity;
import com.vicene.gmail.order.service.OrderService;
import com.vicene.gmail.common.utils.PageUtils;
import com.vicene.gmail.common.utils.R;
import reactor.core.publisher.Flux;


/**
 * 订单
 *
 * @author vicene
 * @email 505543479@qq.com
 * @date 2020-10-16 15:14:50
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/status/{orderSn}")
    //@RequiresPermissions("order:order:list")
    public R getOrderStatus(@PathVariable("orderSn") String orderSn){
        OrderEntity orderEntity = orderService.getOrderByOrderSn(orderSn);
        return R.ok().put("data", orderEntity);
    }

    @RequestMapping("/sendMessage")
    @ResponseBody
    //@RequiresPermissions("order:order:list")
    public String sendMessage(){
        for(int i = 0 ; i <10 ; i++){
            if(i%2==0){
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setBillContent("111");
                orderEntity.setCommentTime(new Date());
                orderEntity.setCouponId(1L);
                rabbitTemplate.convertAndSend("hello-java-exchange" , "hello.java" , orderEntity ,new CorrelationData(UUID.randomUUID().toString()));
            }else{
                OrderItemEntity orderItemEntity = new OrderItemEntity();
                orderItemEntity.setCategoryId(1L);
                rabbitTemplate.convertAndSend("hello-java-exchange" , "hello2.java" , orderItemEntity , new CorrelationData(UUID.randomUUID().toString()));
            }

        }
        return "ok";
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @PostMapping("/listWithItem")
    //@RequiresPermissions("order:order:list")
    public R listWithItem(@RequestBody Map<String, Object> params){
        PageUtils page = orderService.queryPageWithItem(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
